package cn.vcjf.washcar.server.controller;


import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.server.entity.ServiceTime;
import cn.vcjf.washcar.server.service.ServiceTimeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-18
 */
@Controller
@RequestMapping("/server/serviceTime")
public class ServiceTimeController {
	@Resource
	private ServiceTimeService serviceTimeService;

	@RequestMapping("/addServiceTime")
	@ResponseBody
	public TableResult addServiceTime(ServiceTime serviceTime) {
		serviceTime.preInsert();
		boolean b = serviceTimeService.insert(serviceTime);
		return TableResult.Result(b);
	}

	@RequestMapping("/addBatchServiceTime")
	@ResponseBody
	public TableResult addBatchServiceTime(ServiceTime serviceTime) throws ParseException {

		List<ServiceTime> serviceTimeList = new ArrayList<>();
		LocalTime time = LocalTime.of(0, 0, 0);
		DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm");
		for (int i = 0; i < 24; i++) {
			ServiceTime serviceTimeTemp = new ServiceTime();
			serviceTimeTemp.setDate(serviceTime.getDate());
			serviceTimeTemp.setStatus(serviceTime.getStatus());
			serviceTimeTemp.setStartTime(time.format(format));
			time = time.plusHours(1);
			serviceTimeTemp.setEndTime(time.format(format));
			serviceTimeTemp.preInsert();
			serviceTimeList.add(serviceTimeTemp);
		}
		boolean b = serviceTimeService.insertBatch(serviceTimeList);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateServiceTime")
	@ResponseBody
	public TableResult updateServiceTime(ServiceTime serviceTime) {
		serviceTime.preUpdate();
		boolean b = serviceTimeService.updateById(serviceTime);
		return TableResult.Result(b);
	}

	@RequestMapping("/findServiceTimeOne")
	@ResponseBody
	public TableResult findServiceTimeOne(ServiceTime serviceTime) {

		EntityWrapper<ServiceTime> wrapper = serviceTime.getQueryWrapper();

		ServiceTime result = serviceTimeService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findServiceTimeList")
	@ResponseBody
	public TableResult findServiceTimeList(ServiceTime serviceTime) {

		EntityWrapper<ServiceTime> wrapper = serviceTime.getQueryWrapper();

		Page<ServiceTime> page = serviceTime.getPageInfo();
//		startTimeBegDate','startTimeEndDate','endTimeBegDate','endTimeEndDate
		if(StringUtils.isNotBlank(serviceTime.getStartTimeBegDate())){
			wrapper.ge("startTime",serviceTime.getStartTimeBegDate());
		}
		if(StringUtils.isNotBlank(serviceTime.getStartTimeEndDate())){
			wrapper.le("startTime",serviceTime.getStartTimeEndDate());
		}
		if(StringUtils.isNotBlank(serviceTime.getEndTimeBegDate())){
			wrapper.ge("endTime",serviceTime.getEndTimeBegDate());
		}
		if(StringUtils.isNotBlank(serviceTime.getEndTimeEndDate())){
			wrapper.le("endTime",serviceTime.getEndTimeEndDate());
		}

		wrapper.orderBy("createTime",false);
		page = serviceTimeService.selectPage(page, wrapper);

		return TableResult.Result(page);

	}

	@RequestMapping("/findWashTimeList")
	@ResponseBody
	public TableResult findWashTimeList() {

		List<Map<String, Object>> result = serviceTimeService.findWashTimeList();

		return TableResult.Result(result);

	}

	@RequestMapping("/deleteServiceTimeByIds")
	@ResponseBody
	public TableResult deleteServiceTimeByIds(String... ids) {
		List<String> list = Arrays.asList(ids);
		boolean b = serviceTimeService.deleteBatchIds(list);
		return TableResult.Result(b);
	}
}
